package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.Data
import com.cheyun.netsalev3.bean.FollowParam
import com.cheyun.netsalev3.bean.ReceptionParam
import com.cheyun.netsalev3.bean.drive.ReserveInfo
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject

class PassengerReceptionPageRepository(var stime:String,var etime:String) : BasePageRepository<Data>() {
    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<Data>) -> Unit) {
        ApiClient.getInstance().getPassenger(1,stime,etime)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ReceptionParam>>() {
                    override fun onSuccess(t: BaseResp<ReceptionParam>) {
                        t.data?.rows?.apply {
                            total.postValue(t.data!!.total)
                            callback(this)
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }


                })
    }

    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, Data>) {
        ApiClient.getInstance().getPassenger(params.key,stime,etime)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ReceptionParam>>() {
                    override fun onSuccess(t: BaseResp<ReceptionParam>) {
                        t.data?.rows?.apply {
                            callback.onResult(this, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, Data>) {
        ApiClient.getInstance().getPassenger(params.key,stime,etime)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ReceptionParam>>() {
                    override fun onSuccess(t: BaseResp<ReceptionParam>) {
                        t.data?.rows?.apply {
                            callback.onResult(this, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    /**
     * 分享
     */
    fun shareData(id: String, shareuid: String) {
        ApiClient.getInstance().getUpdate(id, shareuid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        dataSourceFactory.sourceLiveData.value?.invalidate()
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    /**
     *  客流接待-接待跟进
     */
    fun getfollow(id: Int, moth: (it: FollowParam) -> Unit) {
        ApiClient.getInstance().getfollow(id)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<FollowParam>>() {
                    override fun onSuccess(t: BaseResp<FollowParam>) {
                        t.data?.apply {
                            this.msg = t.msg ?: ""
                            moth(this)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    fun getScanResult(phone:String,rid:String,kFunction1: (item: ReserveInfo) -> Unit){
        ApiClient.getInstance().getReserveInfo("",phone,"",rid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ReserveInfo>>() {
                    override fun onSuccess(t: BaseResp<ReserveInfo>) {
                        if (t.state == "y") {
                            kFunction1(t.data!!)
                        }
                    }
                    override fun onError(ex: ApiException) {

                    }


                })
    }

}